﻿using System; 
using System.Text;
using System.Collections.Generic; 
using System.Data;
using System.Data.SqlClient;
using ART.Data;
using ART.Common;
using ART.Model;
using ART.Cache;

namespace ART.BLL {
  //SchoolDic
 	public partial class B_SchoolDic
	{
   		
		public B_SchoolDic()
		{}
        #region __________基础方法__________
		/// <summary>
		/// 删除指定缓存
		/// </summary>
		public void ClearCache(string key)
		{
		    string CacheKey = "BLL/SchoolDic";
			if(!string.IsNullOrEmpty(key)){
			    CacheKey+="/"+key;
			}
			LRSCache.GetCacheService().RemoveObject(CacheKey);
		}
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void  Add(ART.Model.M_SchoolDic model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into SchoolDic(");			
            strSql.Append("SchoolName,AreaId");
			strSql.Append(") values (");
            strSql.Append("@SchoolName,@AreaId");            
            strSql.Append(") ");            
            	
			#region __________参数__________	
			SqlParameter[] parameters = {        
                       new SqlParameter("@SchoolName", SqlDbType.NVarChar,150) ,            
                        new SqlParameter("@AreaId", SqlDbType.Int)            
              
            };
			
			                            
            parameters[0].Value = model.SchoolName;                        
            parameters[1].Value = model.AreaId;            			
            #endregion
			            SqlHelper.ExecNonQuery(strSql.ToString(),parameters);
            		
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ART.Model.M_SchoolDic model)
		{
		    StringBuilder strSql=new StringBuilder();
			#region __________语句__________
			strSql.Append("update SchoolDic set ");
			                        
            strSql.Append(" Id = @Id , ");                                    
            strSql.Append(" SchoolName = @SchoolName , ");                                    
            strSql.Append(" AreaId = @AreaId  ");            			
			strSql.Append(" where Id=@Id  ");
			
			#endregion	
			
			#region __________参数__________	
            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@SchoolName", SqlDbType.NVarChar,150) ,            
                        new SqlParameter("@AreaId", SqlDbType.Int)             
              
            };
            			            
            parameters[0].Value = model.Id;                        
            parameters[1].Value = model.SchoolName;                        
            parameters[2].Value = model.AreaId;            			
            #endregion
			
            int rows=SqlHelper.ExecNonQuery(strSql.ToString(),parameters);
			if (rows > 0)
			{
                string cachekey = "info_" + model.Id;
                ClearCache(cachekey);
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from SchoolDic ");
			strSql.Append(" where Id=@Id ");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)			};
			parameters[0].Value = Id;

			int rows=SqlHelper.ExecNonQuery(strSql.ToString(),parameters);

            string cachekey = "info_" + Id;
            ClearCache(cachekey);
			
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

 	    /// <summary>
 	    /// 得到一个对象实体
 	    /// </summary>
 	    public ART.Model.M_SchoolDic GetModel(int Id)
 	    {
 	        string CacheKey = "BLL/SchoolDic/info_" + Id;
 	        object objModel = LRSCache.GetCacheService().RetrieveObject(CacheKey);
 	        if (objModel == null)
 	        {
 	            StringBuilder strSql = new StringBuilder();
 	            strSql.Append("select * from SchoolDic ");
 	            strSql.Append(" where Id=@Id ");

 	            SqlParameter[] parameters =
 	                {
 	                    new SqlParameter("@Id", SqlDbType.Int, 4)
 	                };
 	            parameters[0].Value = Id;


 	            ART.Model.M_SchoolDic model = new ART.Model.M_SchoolDic();
 	            DataSet ds = SqlHelper.ExecDataSet(strSql.ToString(), parameters);

 	            if (ds.Tables[0].Rows.Count > 0)
 	            {
 	                #region __________实体赋值__________

 	                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
 	                {
 	                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
 	                }
 	                model.SchoolName = ds.Tables[0].Rows[0]["SchoolName"].ToString();
 	                model.AreaId = Utils.StrToInt(ds.Tables[0].Rows[0]["AreaId"].ToString(),0);


 	                #endregion

 	                LRSCache.GetCacheService().AddObject(CacheKey, model, 2*60*60);
 	                return model;
 	            }
 	            else
 	            {
 	                return null;
 	            }

 	        }
 	        return (ART.Model.M_SchoolDic) objModel;
 	    }

 	    /// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return GetList(0,strWhere,"");
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return GetList(0,"*",strWhere,filedOrder);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string FieldName,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" "+FieldName+" FROM SchoolDic ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			if(filedOrder.Trim()!="")
			{
				strSql.Append(" order by " + filedOrder);
			}
			return SqlHelper.ExecDataSet(strSql.ToString());
		}
#endregion

      #region __________自定义方法__________

      #endregion 
	}
}

